<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="二维码类型" prop="isTemp">
        <el-radio v-model="dataForm.isTemp" :label="true">临时</el-radio>
        <el-radio v-model="dataForm.isTemp" :label="false">永久</el-radio>
        <div>
          <a v-show="!dataForm.isTemp" class="text-warning" target="_blank" href="https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html">注意永久二维码上限10万个，且暂时无法删除旧的二维码</a>
        </div>
      </el-form-item>

      <el-form-item label="场景值" prop="sceneStr">
        <el-input v-model="dataForm.sceneStr" placeholder="任意字符串" maxlength="64" />
      </el-form-item>
      <el-form-item v-if="dataForm.isTemp" label="失效时间/秒" prop="expireSeconds">
        <el-input v-model="dataForm.expireSeconds" placeholder="单位：秒，最大2592000（30天）" />
        <div>最大30天，当前设置：<span class="text-warning">{{ dataForm.expireSeconds/(24*3600) }}天</span></div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { wxCreateqrcode } from '../api/wx_qrcode'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        isTemp: true,
        sceneStr: '',
        expireSeconds: 2592000
      },
      dataRule: {
        isTemp: [
          { required: true, message: '二维码类型不能为空', trigger: 'blur' }
        ],
        sceneStr: [
          { required: true, message: '场景值ID不能为空', trigger: 'blur' }
        ],
        expireSeconds: [
          { required: true, message: '该二维码失效时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          wxCreateqrcode({
            time_type: this.dataForm.isTemp ? 0 : 1,
            scene: this.dataForm.sceneStr,
            expireSeconds: this.dataForm.expireSeconds
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
